package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.MobileExhibitors;
import com.ruoyi.business.domain.MobileUserCollection;
import com.ruoyi.business.mapper.MobileExhibitorsMapper;
import com.ruoyi.business.mapper.MobileUserCollectionMapper;
import com.ruoyi.business.service.IMobileUserCollectionService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 我的收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-14
 */
@Service
public class MobileUserCollectionServiceImpl extends ServiceImpl<MobileUserCollectionMapper, MobileUserCollection> implements IMobileUserCollectionService {

    @Autowired
    private MobileUserCollectionMapper collectionMapper;
    @Autowired
    private MobileExhibitorsMapper exhibitorsMapper;

    /**
     * @param collection
     * @return void
     * @Author liumaotong
     * @Description 加入收藏
     * @Date 2024/6/14
     * @Param [collection]
     */
    @Override
    public void addCollection(MobileUserCollection collection) {
        QueryWrapper<MobileUserCollection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.lambda().eq(MobileUserCollection::getOpenId,collection.getOpenId());
        collectionQueryWrapper.lambda().eq(MobileUserCollection::getExhibitorsId,collection.getExhibitorsId());
        MobileUserCollection userCollection = collectionMapper.selectOne(collectionQueryWrapper);

        if(null != userCollection){
            return;
        }

        collection.setCreateTime(new Date());
        collection.setUpdateTime(new Date());
        collectionMapper.insert(collection);
    }

    /**
     * @param collection
     * @return void
     * @Author liumaotong
     * @Description 取消收藏
     * @Date 2024/6/14
     * @Param [collection]
     */
    @Override
    public void deleteCollection(MobileUserCollection collection) {
        QueryWrapper<MobileUserCollection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.lambda().eq(MobileUserCollection::getOpenId,collection.getOpenId());
        collectionQueryWrapper.lambda().eq(MobileUserCollection::getExhibitorsId,collection.getExhibitorsId());
        MobileUserCollection userCollection = collectionMapper.selectOne(collectionQueryWrapper);

        if(null == userCollection){
            return;
        }

        collectionMapper.deleteById(userCollection.getId());
    }

    /**
     * @param collection
     * @return java.util.List<com.ruoyi.business.domain.MobileExhibitors>
     * @Author liumaotong
     * @Description 收藏列表
     * @Date 2024/6/14
     * @Param [collection]
     */
    @Override
    public List<MobileExhibitors> collectionList(MobileUserCollection collection) {
        QueryWrapper<MobileUserCollection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.lambda().eq(MobileUserCollection::getOpenId,collection.getOpenId());
        List<MobileUserCollection> userCollectionList = collectionMapper.selectList(collectionQueryWrapper);
        List<Long> exhibitorIds = userCollectionList.stream().map(c -> c.getExhibitorsId()).collect(Collectors.toList());

        QueryWrapper<MobileExhibitors> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MobileExhibitors::getDelFlag,"0");
        queryWrapper.lambda().in(MobileExhibitors::getId,exhibitorIds);
        return exhibitorsMapper.selectList(queryWrapper);
    }
}
